using System.Drawing;
using GTA;
using NativeUI;

namespace CustomPeds.Menus;

public class OnFootMainMenu : Script
{
    public static UIMenu menu = new UIMenu("", TextSetting.of_Menu, new Point(0, -107));
    private static bool inited;

    public OnFootMainMenu()
    {
        if (!inited)
        {
            Main.pool.Add(menu);
            Build();
            menu.OnMenuClose += OnMenuClose;
            inited = true;
        }
    }

    public static void Open()
    {
        if (!inited) return;
        Build();
        menu.Visible = true;
        menu.RefreshIndex();
    }

    public static void Build()
    {
        menu.Clear();
        menu.SetBannerType(new UIResRectangle());

        var chat = new UIMenuItem(TextSetting.of_MenuItem_Chat);
        chat.Activated += (m, i) =>
        {
            if (Main.P2.Character != null)
            {
                Main.P2.Character.PlayAmbientSpeech2("GENERIC_HI", immediately: true);
            }
            Main.OnMainMenuHidden();
            menu.Visible = false;
        };

        menu.AddItem(chat);

        // 仅在与 Reference 相同的条件下显示并绑定“Special”子菜单（增加空值保护）
        bool p1InVeh = Main.P1 != null && Main.P1.Character != null && Main.P1.Character.IsInVehicle();
        if (!p1InVeh && Main.IsGirlfriendInNear(3f))
        {
            var special = new UIMenuItem(TextSetting.of_MenuItem_Special);
            menu.AddItem(special);
            menu.BindMenuToItem(OnFootSexMenu.menu, special);
        }
        menu.RefreshIndex();
    }

    private static void OnMenuClose(UIMenu sender)
    {
        // 不改变可见性，避免重复触发；仅记录日志
        try { LogHelper.WriteInfo("OnFootMainMenu closed"); } catch { }
    }
}


